package org.victoriest.server.pipeline;

import static org.jboss.netty.channel.Channels.pipeline;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.serialization.ClassResolvers;
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.HashedWheelTimer;
import org.springframework.stereotype.Service;

@Service("pipeLineFactory")
public class EndrawingPipeLineFactory implements ChannelPipelineFactory {

	@Resource(name = "endrawingHandlerServ")
	private ChannelHandler endrawingHandlerServ;

	private IdleStateHandler idleStateHandler;

	@Override
	public ChannelPipeline getPipeline() throws Exception {
		idleStateHandler = new IdleStateHandler(new HashedWheelTimer(),
				6000, 0, 0, TimeUnit.MILLISECONDS);

		ChannelPipeline pipeline = pipeline();

		pipeline.addLast("encoder", new ObjectEncoder());
		pipeline.addLast(
				"decoder",
				new ObjectDecoder(ClassResolvers.cacheDisabled(getClass()
						.getClassLoader())));
		// and then business logic.

		pipeline.addLast("idle", idleStateHandler);// 添加心跳机制

		pipeline.addLast("endrawing", endrawingHandlerServ);

		return pipeline;
	}
}
